package uf.audit.demo.controller;

import java.io.UnsupportedEncodingException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

import uf.audit.tax.platform.core.annotation.Controller;
import uf.audit.tax.platform.core.spring.bean.ILogInfo;
import uf.audit.tax.platform.core.spring.bean.User;
import uf.audit.tax.platform.util.Utils;
import uf.audit.tax.platform.util.encryption.RSAUtil;
import uf.audit.tax.platform.web.UFBaseController;

@Controller(url = "/pub", view = "/pages/pub")
public class PublicController extends UFBaseController {

	private static final Logger log = Logger.getLogger(PublicController.class);

	public static String loginPage = "/pub/login";

	public void login() {
		ILogInfo logInfo = Utils.getApplicationContext().getBean("LogInfo", ILogInfo.class);
		if (!logInfo.login(this)) {
			renderError(403);
		}
	}

	public void a() {
		String postData = getPostData();
		System.out.println(postData);
		renderSuccess();
	}

	public void b() {
		render("index.html");
	}

	public void logout() {
		ILogInfo logInfo = Utils.getApplicationContext().getBean("LogInfo", ILogInfo.class);
		if (!logInfo.login(this)) {
			renderError(403);
			return;
		}
	}

	public void getPublicKey() {
		String ori = "987654321";
		String publicKey = RSAUtil.rsaKeyStrMap.get("publicKey");
		Map<String, String> m = new HashMap<String, String>();
		m.put("ori", ori);
		m.put("res", publicKey);
		renderJson(m);
		System.out.println("pk：" + publicKey);
		System.out.println("加密前的字符：" + ori);
		byte[] ori2byte;
		try {
			ori2byte = ori.getBytes("utf-8");
			byte[] enc2byte = RSAUtil.RSAEncode((PublicKey) RSAUtil.rsaKeyMap.get(RSAUtil.PUBLIC_KEY), ori2byte);
			String enc2base64 = Base64.encodeBase64String(enc2byte);
			System.out.println("加密后的字符：" + enc2base64);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void rsaDecode() {
		String str = getPostData();
		System.out.println("解密前的字符：" + str);
		String jm = RSAUtil.RSADecode((PrivateKey) RSAUtil.rsaKeyMap.get(RSAUtil.PRIVATE_KEY),
				Base64.decodeBase64(str));
		System.out.println("解密后的字符：" + jm);
		renderSuccess();
	}

	private static String mUserLogin;

	public void checkUser() {
		if (mUserLogin == null) {
			Properties p;
			try {
				p = Utils.getProperties("/sysinfo.properties");
				mUserLogin = p.getProperty("muserlogin");
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (mUserLogin == null) {
				mUserLogin = "denied";
			}
		}
		log.info("mUserLogin->" + mUserLogin);
		if (mUserLogin.equalsIgnoreCase("allow")) {
			User user = Utils.getApplicationContext().getBean("User", User.class);
			boolean valid = user.checkUserToken(this);
			if (!valid) {
				Utils.setLogout(this);
				renderError();
				return;
			}
			renderSuccess();
		} else {
			renderSuccess();
		}
	}
}
